from django import forms
from django.core.exceptions import ValidationError
from user.models import VSession, User, WarZone, Company, IPSystem
import pdb


def token_check(value):
    # 验证token必须存在
    sessionList = VSession.objects.filter(token=value)
    if not sessionList.exists():
        errMsg = 'token Error'
        isValidOk = False
        raise ValidationError(errMsg)
    else:
        session = sessionList[0]


def IP_check(value):
    # 验证IP必须存在
    IPList = IPSystem.objects.filter(userIP=value)
    if not IPList.exists():
        errMsg = 'IP Error'
        isValidOk = False
        raise ValidationError(errMsg)
    else:
        userIP = IPList[0]


def userId_check(value):
    # 验证单位id必须存在
    if value != -1:
        is_num_by_except(value)
        user = User.objects.filter(pk=value)
        if not user.exists():
            errMsg = 'userId Error'
            raise ValidationError(errMsg)


def is_num_by_except(num):
    try:
        int(num)
        return True
    except ValueError:
        errMsg = 'value not int'
        raise ValidationError(errMsg)


def maxItem_check(value):
    if value < 1:
        errMsg = 'maxItem Error'
        raise ValidationError(errMsg)


def page_check(value):
    if value < 1:
        errMsg = 'page Error'
        raise ValidationError(errMsg)


class IPOperateForm(forms.Form):
    token = forms.CharField(max_length=100, validators=[token_check])
    userIP = forms.GenericIPAddressField(required=False, validators=[IP_check])
    operating = forms.CharField(max_length=20)
    userId = forms.CharField(max_length=20, validators=[userId_check])


class SystemStatusGetForm(forms.Form):
    token = forms.CharField(required=False, max_length=100, validators=[token_check])
    maxItem = forms.IntegerField(required=False, validators=[maxItem_check])
    page = forms.IntegerField(required=False, validators=[page_check])
    group = forms.CharField(required=False, max_length=20)
    storehouseName = forms.CharField(max_length=20, required=False)
    storeIP = forms.CharField(max_length=20, required=False)
    storePort = forms.CharField(max_length=20, required=False)
    ram = forms.CharField(required=False, max_length=20)
    cpu = forms.CharField(required=False, max_length=20)
    ramTotal = forms.CharField(required=False, max_length=20)
    caliche = forms.CharField(required=False, max_length=20)
    calicheTotal = forms.CharField(required=False, max_length=20)
    ramUsed = forms.CharField(required=False, max_length=20)
    calicheUsed = forms.CharField(required=False, max_length=20)
    # def clean(self):
    #     group = self.cleaned_data['group']
    #     if group != '' and (group not in ['0', '1','2']):
    #         errMsg = 'group Error'
    #         raise ValidationError(errMsg)
    #     return self.cleaned_data


class SystemScreenForm(forms.Form):
    token = forms.CharField(max_length=100, validators=[token_check])
    maxItem = forms.IntegerField(validators=[maxItem_check])
    page = forms.IntegerField(validators=[page_check])
    storehouseName = forms.CharField(max_length=20, required=False)
